package com.hjj.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hjj.springboot.entity.HealthPlan;
import com.hjj.springboot.entity.UserHealthInfo;
import com.hjj.springboot.service.IHealthPlanService;
import com.hjj.springboot.service.IUserHealthInfoService;
import com.hjj.springboot.service.MealPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/api/meal-plans")
public class MealPlanController {

    @Autowired
    private MealPlanService mealPlanService;

    @Autowired
    private IUserHealthInfoService userHealthInfoService;

    @Autowired
    private IHealthPlanService healthPlanService;

    @PostMapping("/generate/{openid}")
    public String generateMealPlan(@RequestParam(required = false) String date, @PathVariable String openid) {
        UserHealthInfo user = userHealthInfoService.getOne(new QueryWrapper<UserHealthInfo>().eq("uid", openid));
        HealthPlan plan = healthPlanService.getOne(new QueryWrapper<HealthPlan>().eq("uid", openid).eq("isOngoing",true));
        int calories = plan.getDailyCalories();
        List<String> allergens = Collections.singletonList(user.getAllergens());
        return mealPlanService.getDailyPlan(date, calories, allergens);
    }
}
